/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import BancoDados.Cliente;
import BancoDados.Profissional;
import BancoDados.Usuario;
import javax.servlet.annotation.WebServlet;


/**
 *
 * @author Pablo
 */
@WebServlet(name="ServletLogin", urlPatterns={"/ServletLogin"})
public class ServletLogin extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
        } finally {
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession sessao = request.getSession();
		sessao.invalidate();

		response.sendRedirect("index.jsp");
		
		
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {


        Usuario user = new Usuario();

        String login = req.getParameter("login");
        String senha = req.getParameter("senha");

        
        user = Usuario.Login(login, senha);

        if (user == null) {

            res.getWriter().println("Logim Inválido!!!");
            res.sendRedirect("LoginUsers.jsp");

        } else {
			
			HttpSession sessao = req.getSession();
			
            if (user.getTipo().equalsIgnoreCase("C")) {

				Cliente cliente = new Cliente();
				cliente = user.getCliente();
				sessao.setAttribute("cliente",cliente);
				res.sendRedirect("PerfilCliente.jsp");
            }

			else if (user.getTipo().equalsIgnoreCase("P")) {
				
				Profissional profissional = new Profissional();
				profissional = user.getProfissional();
				sessao.setAttribute("profissional",profissional);
				res.sendRedirect("index.jsp");
				//res.sendRedirect("PerfilProfissional.jsp");
			}

			else if (user.getTipo().equalsIgnoreCase("O")) {

				sessao.setAttribute("operadorsistema",true);
				res.sendRedirect("index.jsp");
			}

        }
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
